package com.tubang.logistics.controller;

import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.tubang.common.util.BeanMapper;
import com.tubang.common.util.GsonQuick;
import com.tubang.common.util.ResultData;
import com.tubang.component.util.RequestUtil;
import com.tubang.logistics.entity.PlantformManagerEntity;
import com.tubang.logistics.service.IPlantformManagerService;
import com.tubang.logistics.vo.PlantformManagerVo;

/**
 * <p>
 * 地址表 前端控制器
 * </p>
 * @author wsl
 * @since 2017-12-28
 */
@RestController
@RequestMapping("/plantformManager")
public class PlantformManagerController {
	private Logger logger = LoggerFactory.getLogger(getClass());
	
	@Resource
	private IPlantformManagerService plantformManagerService;
	
	/**
	 * @Title insertOrUpdate 
	 * @Description 平台设置
	 * @param  request
	 * @param  response
	 * @return ResultData 返回类型 
	 * @throws
	 */
	@PostMapping(value = "insertOrUpdate")
	public ResultData insertOrUpdate(HttpServletRequest request, HttpServletResponse response) {
		// 获取数据
		String receivePostJson = RequestUtil.receivePostJson(request);
		PlantformManagerVo form = GsonQuick.toObject(receivePostJson, PlantformManagerVo.class);
		try {
			PlantformManagerEntity plantformManagerEntity = BeanMapper.map(form, PlantformManagerEntity.class);
			plantformManagerEntity.setUpdateTime(new Date());
			plantformManagerService.insertOrUpdate(plantformManagerEntity);
			logger.info("[plantformManager]<== method[insertOrUpdate],新增或者修改的数据为 ：[{}] ",
					GsonQuick.getGson().toJson(plantformManagerEntity));
			return ResultData.result(true).setData(plantformManagerEntity);
		} catch (Exception e) {
			logger.error("[plantformManager]<== method[insertOrUpdate], 新增或者修改失败 :[{}]", e.getMessage());
			e.printStackTrace();
			return ResultData.result(false).setData(e);
		}
	}
	
	@GetMapping(value = "selectById")
	public ResultData selectById(@RequestParam(required = true) String plantformId) {
		// 获取数据
		try {
			logger.info("[plantformManager]==> method[insertOrUpdate],查询id ：[{}] ",plantformId);
			PlantformManagerEntity entity = plantformManagerService.selectById(plantformId);
			return ResultData.result(true).setData(entity);
		} catch (Exception e) {
			logger.error("[plantformManager]<== method[insertOrUpdate], 失败原因为 :[{}]", e.getMessage());
			e.printStackTrace();
			return ResultData.result(false).setData(e);
		}
	}

}
